package com.wuxinwudai.android.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * ZipUtils 类提供 zip 文件压缩和解压缩功能
 * @author 吾心无待 于 2015年12月25日
 */
public class ZipUtils {
    /**
     * 将一个文件压缩进 ZIP 文件中
     * @param source 源文件
     * @param zipFile 目标文件
     * @return 压缩是否成功
     */
    public static boolean compressFile(File source,File zipFile){
        boolean isOk = true;
        ZipOutputStream zipOutputStream = null;
        BufferedInputStream bufferedInputStream = null;
        try {
            if (!zipFile.exists()) {
                File dir = new File(zipFile.getParent());
                if (!dir.exists()) {
                    isOk = dir.mkdirs();
                    if (!isOk) return false;
                }
                if (!zipFile.exists()) {
                    isOk = zipFile.createNewFile();
                    if (!isOk) return false;
                }
            }
            zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
            bufferedInputStream = new BufferedInputStream(new FileInputStream(source), 4096);
            ZipEntry entry = new ZipEntry(source.getName());
            zipOutputStream.putNextEntry(entry);
            byte[] buffer = new byte[1024 * 4];//缓冲区4K
            int len = 0;
            while (-1 != (len = bufferedInputStream.read(buffer))) {
                zipOutputStream.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
            isOk = false;
        } finally {
            try {
                if (zipOutputStream != null) zipOutputStream.close();
                if (bufferedInputStream != null) bufferedInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return isOk;
    }

    /**
     * 解压缩功能.
     * 将 zipFile 文件解压到 folderPath 目录下.
     * @param sourceFile  要解压缩的 ZIP 文件
     * @param targetFile 要解压缩到的目标文件
     * @return 解压缩是否成功
     */
    public static boolean decompressFile(File sourceFile, File targetFile) {
        boolean isOk = true;
        ZipFile zipFile = null;
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            File dir = new File(targetFile.getParent());
            if (!dir.exists()) {
                isOk = dir.mkdirs();
                if (!isOk) return false;
            }
            if (!targetFile.exists()) {
                isOk = targetFile.createNewFile();
                if (!isOk) return false;
            }
            zipFile = new ZipFile(sourceFile);
            ZipEntry entry = zipFile.getEntry(targetFile.getName());
            bufferedInputStream = new BufferedInputStream(zipFile.getInputStream(entry));
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(targetFile));
            byte[] buffer = new byte[1024 * 4];
            int len = 0;
            while (-1 != (len = bufferedInputStream.read(buffer))) {
                bufferedOutputStream.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
            isOk = false;
        } finally {
            try {
                if (bufferedOutputStream != null) bufferedOutputStream.close();
                if (bufferedInputStream != null) bufferedInputStream.close();
                if (zipFile != null) zipFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return isOk;
    }
}
